package com.becca.cake.controller.users;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.users.UserCouponModel;
import com.becca.cake.model.users.UserFollowHolidayModel;
import com.becca.cake.model.users.UsersModel;
import com.becca.cake.service.users.IUserManagerService;
import com.becca.cake.util.JsonUtil;
import com.becca.cake.util.StringUtil;

/**
 * 用户管理
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年5月16日 下午11:35:25
 */
@Controller
@RequestMapping("/userManager")
public class UserManagerAction extends BaseAction {
    private final static Log    logger = LogFactory.getLog(UserManagerAction.class);

    @Autowired
    private IUserManagerService iUserService;

    /**
     * 查询用户数据
     * @param usersModel
     * @return
     */
    @RequestMapping(value = "/selectAllUsers", method = RequestMethod.POST)
    @ResponseBody
    public String selectAllUsers(UsersModel usersModel, String userStartDate, String userEndDate) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("usersModel", usersModel);
        map.put("pagingBean", page);
        map.put("userStartDate", userStartDate);
        map.put("userEndDate", userEndDate);
        try {
            // stemp1:查询所有用户数据
            List<UsersModel> list = this.iUserService.selectAllUsers(map);
            // stemp1:查询用户数据总数
            Long count = this.iUserService.selectAllUsersCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改用户
     * @param request
     * @param usersModel
     * @return
     */
    @RequestMapping(value = "/addOrUpdateUser", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdateUser(HttpServletRequest request, UsersModel usersModel) {
        //step1:获取图片file
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile files = multipartRequest.getFile("upload");
        try {
            //step2:添加或修改主题信息
            this.iUserService.addOrUpdateUser(usersModel, files, getTempFoderPath(),
                this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("操作用户信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 判断用户手机号是否已存在
     * @param mobileNo
     * @return
     */
    @RequestMapping(value = "/valideMobileIsExist", method = RequestMethod.POST)
    @ResponseBody
    public String valideMobileIsExist(@RequestParam(value = "mobileNo", required = false) String mobileNo) {
        try {
            return this.iUserService.valideMobileIsExist(mobileNo);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据用户手机号查询优惠券
     * @param userMobileNo
     * @return
     */
    @RequestMapping(value = "/selectUserCouponsByMobileNo", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserCouponsByMobileNo(@RequestParam(value = "mobileNo", required = false) String mobileNo) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mobileNo", mobileNo);
        map.put("pagingBean", page);
        try {
            List<UserCouponModel> list = this.iUserService.selectUserCouponsByMobileNo(map);
            Long count = this.iUserService.selectUserCouponsByMobileNoCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }

    }

    /**
     * 查询节日假期数据
     * @param usersModel
     * @return
     */
    @RequestMapping(value = "/selectFollowHolidays", method = RequestMethod.POST)
    @ResponseBody
    public String selectFollowHolidays(@RequestParam Integer userId, @RequestParam Byte status) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("pagingBean", page);
        map.put("status", status);
        try {
            // stemp1:查询所有用户数据
            List<UserFollowHolidayModel> list = this.iUserService.selectFollowHolidays(map);
            // stemp1:查询用户数据总数
            Long count = this.iUserService.selectFollowHolidaysCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据用户id查询用户对象
     * @param userId
     * @return
     */
    @RequestMapping(value = "/selectUserById", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserById(@RequestParam(value = "userId", required = false) Integer userId) {
        try {
            // stemp1:查询用户对象
            UsersModel usersModel = this.iUserService.selectUserById(userId);
            return JsonUtil.writeObjectFormJSON(usersModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 启用/禁用配件
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/updUserStatusById", method = RequestMethod.POST)
    @ResponseBody
    public String updUserStatusById(@RequestParam(value = "userId", required = false) String userId,
                                    @RequestParam(value = "status", required = false) String status) {
        try {
            this.iUserService.updUserStatusById(userId,status,this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("操作用户成功！");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 统计用户 显示图表
     * @param days
     * @return
     */
    @RequestMapping(value = "/selectUserChart", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserChart(@RequestParam(value = "radioDay", required = false)String radioDay,
                                  @RequestParam(value = "userChartStartDate", required = false)String userChartStartDate,
                                  @RequestParam(value = "userChartEndDate", required = false)String userChartEndDate){
        try {
            // stemp1:查询所有用户数据   
            List<UsersModel> list = this.iUserService.selectUserChart(radioDay,userChartStartDate,userChartEndDate);
            // stemp1:查询用户数据总数
            return JsonUtil.writeObjectJSON(list);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
}
